<?php
	// start 取得分类并存入一个数组的函数
	function select_category ($mysql_connection) {
		$query = "select * from `category` order by `name` asc";
		$result = $mysql_connection->query($query);
		$category = array();
		$result_num = $result->num_rows;
		for ($i=0; $i<$result_num; $i++) {
			$row_result = $result->fetch_assoc();
			$category[$row_result['id']] = $row_result['name'];
		}
		return $category;
	}
	// end 取得帖子分类并存入一个数组的函数

	// start 查询所有帖子数量
	function count_table ($mysql_connection,$table_name) {
		$query = "select count(*) as `count` from `".$table_name."`";
		$result = $mysql_connection->query($query);
		$result = $result->fetch_assoc();
		return $result['count'];
	}
	// end 查询所有帖子数量

	// start 根据帖子 ID 查询评论数量
	function count_comment ($article_id, $mysql_connection) {
		$query_comment = "select * from `comment` where `articles_id`=".$article_id;
		$result_comment = $mysql_connection->query($query_comment);
		$num_comment = $result_comment->num_rows;
		return $num_comment;
	}
	// end 根据帖子 ID 查询评论数量
	
	// start 获得文件扩展名
	function extend($file_name) { 
		$retval="";
		$pt=strrpos($file_name, ".");
		if ($pt) $retval=substr($file_name, $pt+1, strlen($file_name) - $pt);
		return ($retval);
	}
	// end 获得文件扩展名

	// start 根据帖子 ID 查询所属分类名称，返回数组
	function get_category($article_id, $mysql_connection) {
		$query = "select `category_id` from `articles_category` where `articles_id`=".$article_id;
		$result_query = $mysql_connection->query($query);
		$category_array = array();
		while ($result_category_array = $result_query->fetch_assoc()) {
			$category_id = $result_category_array['category_id'];
			$query = "select `name` from `category` where `id`=".$category_id." order by `name` asc";
			$result_query_cat = $mysql_connection->query($query);
			$result_category_name = $result_query_cat->fetch_assoc();
			$category_array[$category_id] = $result_category_name['name'];
		}
		return $category_array;
	}
	// end 根据帖子 ID 查询所属分类名称，返回数组

	// start 取得帖子支持和反对的数量
	function get_count_zhichi_fandui($article_id, $mysql_connection) {
		$query = "select `zhichi`,`fandui` from `articles` where `id`=".$article_id;
		$result_query = $mysql_connection->query($query);
		$result_query = $result_query->fetch_assoc();
		return $result_query;
	}
	// end 取得帖子支持和反对的数量

	// start 取得某年某月的帖子数量
	function get_count_article_date($month,$year,$mysql_connection) {
		$query = "select count(*) as `a_count` from `articles` where date_format(`post_time`,'%Y')='".$year."' and date_format(`post_time`,'%m')='".$month."'";
		$result_q = $mysql_connection->query($query);
		$count = $result_q->fetch_assoc();
		$count = $count['a_count'];
		return $count;
	}
	// end 取得某年某月的帖子数量

	// start 根据分类ID获得分类下的帖子数量
	function get_count_article_category($category_id,$mysql_connection) {
		$query = "select count(*) as `a_count` from `articles_category` where `category_id`=".$category_id;
		$result_r = $mysql_connection->query($query);
		$a_count = $result_r->fetch_assoc();
		$a_count = $a_count['a_count'];
		return $a_count;
	}
	// end 根据分类ID获得分类下的帖子数量
?>
